import numeral from 'numeral';
import { format } from './datetime';
import moment from 'moment';
import { message } from 'antd';
import _ from 'lodash';

export function renderNumber(fm: string, text: string) {
  if (text === undefined || text === null) {
    return '';
  }
  let num = Number(text);
  let f = '0,0.00';
  let isP = false;
  if (fm === '.2f%') {
    isP = true;
  } else if (fm === '.4f') {
    f = '0,0.0000';
  } else if (fm === '.4f%') {
    f = '0,0.0000';
    isP = true;
  } else if (fm === '.0f') {
    f = '0,0';
  }
  let toVal = '0';
  if (num) {
    num = isP ? num * 100 : num;
    toVal = numeral(num).format(f);
    toVal = isP ? toVal + '%' : toVal;
  }
  return toVal;
}

const organNames = {
  sh: '上海分行',
  tj: '天津分行',
  nj: '南京分行',
  sz: '深圳分行',
  nc: '南昌分行',
  wlmq: '乌鲁木齐分行',
};
const appNames = {
  sh: '绩效考核',
  tj: '报表平台',
  nj: '绩效考核',
  sz: '绩效考核',
  nc: '绩效考核',
  wlmq: '绩效考核',
};
const appFullNames = {
  sh: '绩效考核与数仓数据运用平台',
  tj: '数据分析与报表展示平台',
  nj: '绩效考核及数据综合运用平台',
  sz: '绩效考核及数仓数据运用平台',
  nc: '绩效考核及数仓数据运用平台',
  wlmq: '绩效考核及数仓数据运用平台',
};
const imgNames = {
  sh: 'login_bg.png',
  tj: 'bobjtj.jpeg',
  nj: 'bobnj.jpg',
};
// export function getBankNo() {
//   const name = localStorage.getItem('sysname');
//   return name;
// }
export function getBankName(appNo: string, hasPrev: boolean) {
  if (appNo === undefined || organNames[appNo!] === undefined) {
    return '';
  }
  return (hasPrev ? '北京银行' : '') + organNames[appNo];
}
export function getAppName(appNo: string) {
  if (appNo === undefined || organNames[appNo] === undefined) {
    return '绩效考核';
  }
  return appNames[appNo];
}
export function getAppFullName(appNo: string) {
  if (appNo === undefined || organNames[appNo] === undefined) {
    return '绩效考核与数仓数据运用平台';
  }
  return appFullNames[appNo];
}
export function getImgName(appNo: string) {
  if (appNo === undefined || organNames[appNo] === undefined) {
    return 'login_bg.png';
  }
  return imgNames[appNo];
}

export function getState(path: string, key: string) {
  const state = localStorage.getItem(path + ':' + key);
  if (state === undefined) {
    return null;
  }
  return JSON.parse(state!);
}

export function setState(path: string, key: string, val: any) {
  if (val.paramVals) {
    const curdate = new Date();
    const year = '' + curdate.getFullYear();
    if (val.paramVals.sdate) {
      const sdate = format(val.paramVals.sdate, 'yyyy-MM-dd');
      const y2 = sdate.substring(0, 4);
      if (year !== y2) {
        return;
      }
      localStorage.setItem('rept:sdate', sdate);
    }
    if (val.paramVals.edate) {
      const edate = format(val.paramVals.edate, 'yyyy-MM-dd');
      const y2 = edate.substring(0, 4);
      if (year !== y2) {
        return;
      }
      localStorage.setItem('rept:edate', edate);
    }
  }
  const jVal = JSON.stringify(val);
  localStorage.setItem(path + ':' + key, jVal);
}

export function getParamVals() {
  const sdate = localStorage.getItem('rept:sdate');
  const edate = localStorage.getItem('rept:edate');
  return { paramVals: { sdate, edate, date: edate } };
}

export function convertStrToVal(c: API.Control, str_val: any) {
  let ret_val = undefined;
  if (str_val === undefined || str_val === null || str_val.length === 0) {
    return ret_val;
  }
  if (c.type === 'cascader-select') {
    ret_val = str_val[str_val.length - 1];
  }
  if (c.dataType === 'number' && !c.multiple) {
    if (c.type === 'month') {
      ret_val = str_val.length === 7 ? new Date(str_val + '-01') : new Date(str_val);
      ret_val = Number(format(ret_val, 'yyyyMM'));
    } else if (c.type === 'quarter') {
      ret_val = '' + str_val;
      ret_val = str_val.replace('-', '');
      ret_val = ret_val.replace('Q', '');
      ret_val = Number(ret_val);
    } else {
      ret_val = Number(str_val);
    }
  } else if (c.type === 'date' || c.type === 'datetime') {
    const date = new Date(str_val);
    ret_val = !date
      ? null
      : new Date(date!.getFullYear(), date!.getMonth(), date!.getDate(), 8, 0, 0);
  } else if (c.type === 'quarter' && (c.dataType === 'date' || c.dataType === 'datetime')) {
    const vals = str_val.split('-');
    let dstr = str_val;
    if (vals[1] === 'Q1' || vals[1] === '01' || vals[1] === '02' || vals[1] === '03') {
      dstr = vals[0] + '-03-31';
    } else if (vals[1] === 'Q2' || vals[1] === '04' || vals[1] === '05' || vals[1] === '06') {
      dstr = vals[0] + '-06-30';
    } else if (vals[1] === 'Q3' || vals[1] === '07' || vals[1] === '08' || vals[1] === '09') {
      dstr = vals[0] + '-09-30';
    } else if (vals[1] === 'Q4' || vals[1] === '10' || vals[1] === '11' || vals[1] === '12') {
      dstr = vals[0] + '-12-31';
    }
    ret_val = dstr === null ? null : new Date(dstr);
  } else if (c.type === 'month') {
    if (c.dataType === 'date' || c.dataType === 'datetime') {
      ret_val = str_val.length === 7 ? new Date(str_val + '-01') : new Date(str_val);
    }
  } else {
    ret_val = str_val === undefined ? null : str_val;
  }
  if (c.multiple || c.type === 'checkbox-group' || c.type?.startsWith('range-')) {
    if (str_val instanceof Array) {
      ret_val = str_val.join(',');
    } else {
      ret_val = undefined;
    }
  }
  return ret_val;
}

export function convertValToStr(c: API.Control, v: any) {
  if (v === null || v === undefined || v === '') {
    return undefined;
  }
  let retVal: any = v instanceof Date ? format(v, 'yyyy-MM-dd') : '' + v;
  if (
    c.type === 'date' ||
    c.type === 'datetime' ||
    c.type === 'year' ||
    c.type === 'month' ||
    c.type === 'quarter'
  ) {
    if (c.type === 'year') {
      retVal = retVal + '/01' + '/01';
    } else if (c.type === 'month') {
      if (retVal.length === 6) {
        retVal = retVal.slice(0, 4) + '/' + retVal.slice(4, 6) + '/01';
      } else if (retVal.length === 7) {
        retVal = retVal.slice(0, 4) + '/' + retVal.slice(5, 7) + '/01';
      } else if (retVal.length === 5) {
        retVal = retVal.slice(0, 4) + '/0' + retVal.slice(4, 5) + '/01';
      }
    } else if (c.type === 'date') {
      if (retVal.length === 8) {
        retVal = retVal.slice(0, 4) + '/' + retVal.slice(4, 6) + '/' + retVal.slice(6, 8);
      }
    } else if (c.type === 'quarter') {
      const y = retVal.slice(0, 4);
      let q = '4';
      if (retVal.length === 5) {
        q = retVal.slice(4, 5);
      } else if (retVal.length === 6) {
        q = retVal.slice(5, 6);
      } else if (retVal.length === 10) {
        const curdate = new Date(retVal);
        const month = curdate.getMonth() + 1;
        if (month === 1 || month === 2 || month === 3) {
          q = '1';
        } else if (month === 4 || month === 5 || month === 6) {
          q = '2';
        } else if (month === 7 || month === 8 || month === 9) {
          q = '3';
        }
      }
      let m = '12';
      if (q === '1') {
        m = '03';
      } else if (q === '2') {
        m = '06';
      } else if (q === '3') {
        m = '09';
      }
      return '' + y + '-' + m + '-01';
    }
    const newDate = new Date(retVal);
    if (_.isDate(newDate)) {
      retVal = moment(newDate, 'YYYY-MM-DD');
    } else {
      message.error(`值${retVal}对于类型${c.type}是无效的`);
      return;
    }
  } else if (c.multiple || c.type === 'checkbox-group') {
    retVal = retVal.split(',');
    if (c.dataType === 'number') {
      retVal = retVal.map((rv: string) => Number(rv));
    }
  } else {
    retVal = v;
  }
  return retVal;
}
